package com.fita.admin.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Date;

/**
 *
 * @author sjh
 *
 */
public class FormatUtils {
    
    private static final String DEFAULT_NUMBER_FORMAT = "0.00";

    /**
     * parse date to string
     * 
     * @param date
     * @return
     */
    public String formatDate(Date date) {
        
        if (date == null) {
            return "";
        }
        
        return com.demo2do.core.utils.DateFormatUtils.format(date, "yyyy-MM-dd");
        
    }
    
    /**
     * parse date_time to string
     * 
     * @param date
     * @return
     */
    public String formatDateTime(Date date) {
        
        if (date == null) {
            return "";
        }
        
        return com.demo2do.core.utils.DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss");
        
    }
    
    /**
     * parse time to string
     * 
     * @param date
     * @return
     */
    public String formatTime(Date date) {
        
        if (date == null) {
            return "";
        }
        
        return com.demo2do.core.utils.DateFormatUtils.format(date, "HH:mm:ss");
        
    }
    
    /**
     * return today with pattern=yyyy-MM-dd
     * 
     * @return
     */
    public String today() {
        return com.demo2do.core.utils.DateFormatUtils.format(new Date(), "yyyy-MM-dd");
    }
    
    
    /**
     * Returns a string of default scale({@code two scale}) 
     * 
     * @param parameter
     * @return
     */
    public String formatNumber(BigDecimal parameter) {
        
        if (parameter == null) {
            return "";
        }
        
        DecimalFormat decimalFormat = new DecimalFormat(DEFAULT_NUMBER_FORMAT);
        return decimalFormat.format(parameter);
        
    }
    
    /**
     * Returns a string of specify scale
     * 
     * @param parameter
     * @param scale
     * @return
     */
    public String formatNumber(BigDecimal parameter, int scale) {
        
        if (parameter == null) {
            return "";
        }
        
        BigDecimal result = parameter.setScale(scale, RoundingMode.HALF_UP);
        return result.toString();
        
    }
    
}
